package as.listener;

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import as.dao.SourceRecordDAO;
import as.exception.DeviceException;
import as.service.AttendanceRecordService;
import as.util.BeanGetter;

public class DeviceReader implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent e) {
		

	}

	public void contextInitialized(ServletContextEvent e) {
		
		Timer timer=new Timer();
		String url=e.getServletContext().getRealPath("/WEB-INF/applicationContext.xml");
		ApplicationContext ctx=new  FileSystemXmlApplicationContext(url);
		AttendanceRecordService service=(AttendanceRecordService)ctx.getBean("attendanceRecordService");
		timer.schedule(new ReadTask(service),15000, 30000);

	}
	private class ReadTask extends TimerTask{
		private AttendanceRecordService service	;
public ReadTask(AttendanceRecordService service){
	this.service=service;
}
		@Override
		public void run() {
			
				try {
					service.update();
				} catch (DeviceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			
		}
		
	}

}
